@page "/workflows/instances"
@inherits StudioComponentBase

@using System.Globalization
@using Elsa.Api.Client.Resources.WorkflowDefinitions.Models
@using Elsa.Api.Client.Resources.WorkflowInstances.Enums
@using Elsa.Studio.Workflows.Pages.WorkflowInstances.List.Models
@using Variant = MudBlazor.Variant

<PageTitle>Workflow instances</PageTitle>

<MudContainer MaxWidth="MaxWidth.False">
    <PageHeading Text="Workflow instances"/>

    <div id="instance-file-upload-button-wrapper" class="d-none">
        <MudFileUpload T="IReadOnlyList<IBrowserFile>" FilesChanged="@OnFilesSelected"/>
    </div>

    <MudTable
        @ref="_table"
        T="WorkflowInstanceRow"
        ServerData="LoadData"
        Dense="true"
        Hover="true"
        Elevation="0"
        OnRowClick="@OnRowClick"
        RowStyle="cursor: pointer;"
        MultiSelection="true"
        SelectOnRowClick="false"
        @bind-SelectedItems="_selectedRows">
        <ToolBarContent>
            <MudPaper Class="gap-3 d-flex" Elevation="0">
                <MudMenu EndIcon="@Icons.Material.Filled.KeyboardArrowDown" Label="Bulk actions" Color="Color.Default" Variant="Variant.Filled" AnchorOrigin="Origin.BottomLeft">
                    <MudMenuItem OnClick="OnBulkDeleteClicked">Delete</MudMenuItem>
                    <MudMenuItem>Cancel</MudMenuItem>
                    <MudMenuItem>Retry</MudMenuItem>
                    <MudMenuItem OnClick="@OnBulkExportClicked">Export</MudMenuItem>
                </MudMenu>
                <MudButton StartIcon="@Icons.Material.Filled.FileUpload" Variant="Variant.Filled" Color="Color.Primary" OnClick="@OnImportClicked">Import</MudButton>
            </MudPaper>
            <MudSpacer/>
            <MudStack Row="true">
                <MudSelect
                    T="WorkflowDefinitionSummary"
                    AdornmentIcon="@Icons.Material.Outlined.Window"
                    Label="Workflow"
                    SelectedValues="SelectedWorkflowDefinitions"
                    MultiSelection="true"
                    Dense="true"
                    Clearable="true"
                    AnchorOrigin="Origin.BottomLeft"
                    Variant="Variant.Outlined"
                    ToStringFunc="@(definition => definition.Name)"
                    SelectedValuesChanged="OnSelectedWorkflowDefinitionsChanged">
                    @foreach (var workflowDefinition in WorkflowDefinitions)
                    {
                        <MudSelectItem Value="@workflowDefinition">@workflowDefinition.Name</MudSelectItem>
                    }
                </MudSelect>
                <MudSelect
                    T="WorkflowStatus"
                    Label="Status"
                    AdornmentIcon="@Icons.Material.Outlined.PlayArrow"
                    MultiSelection="true"
                    Variant="Variant.Outlined"
                    AnchorOrigin="Origin.BottomLeft"
                    Dense="true"
                    Clearable="true"
                    SelectedValues="SelectedStatuses"
                    SelectedValuesChanged="OnSelectedStatusesChanged">
                    <MudSelectItem Value="WorkflowStatus.Running"></MudSelectItem>
                    <MudSelectItem Value="WorkflowStatus.Finished"></MudSelectItem>
                </MudSelect>
                <MudSelect
                    T="WorkflowSubStatus"
                    Label="Sub status"
                    AdornmentIcon="@Icons.Material.Outlined.PlayArrow"
                    MultiSelection="true"
                    Variant="Variant.Outlined"
                    AnchorOrigin="Origin.BottomLeft"
                    Dense="true"
                    Clearable="true"
                    SelectedValues="SelectedSubStatuses"
                    SelectedValuesChanged="OnSelectedSubStatusesChanged">
                    <MudSelectItem Value="WorkflowSubStatus.Executing"></MudSelectItem>
                    <MudSelectItem Value="WorkflowSubStatus.Suspended"></MudSelectItem>
                    <MudSelectItem Value="WorkflowSubStatus.Finished"></MudSelectItem>
                    <MudSelectItem Value="WorkflowSubStatus.Faulted"></MudSelectItem>
                    <MudSelectItem Value="WorkflowSubStatus.Cancelled"></MudSelectItem>
                </MudSelect>
            </MudStack>
        </ToolBarContent>
        <HeaderContent>
            <MudTh>ID</MudTh>
            <MudTh>Correlation ID</MudTh>
            <MudTh>Workflow</MudTh>
            <MudTh>Version</MudTh>
            <MudTh>
                <MudTableSortLabel SortLabel="Name" T="WorkflowInstanceRow">Name</MudTableSortLabel>
            </MudTh>
            <MudTh>
                <MudTableSortLabel SortLabel="SubStatus" T="WorkflowInstanceRow">Status</MudTableSortLabel>
            </MudTh>
            <MudTh>
                <MudTableSortLabel SortLabel="IncidentCount" T="WorkflowInstanceRow">Incidents</MudTableSortLabel>
            </MudTh>
            <MudTh>
                <MudTableSortLabel SortLabel="CreatedAt" T="WorkflowInstanceRow" InitialDirection="SortDirection.Descending">Created</MudTableSortLabel>
            </MudTh>
            <MudTh>
                <MudTableSortLabel SortLabel="UpdatedAt" T="WorkflowInstanceRow">Last executed</MudTableSortLabel>
            </MudTh>
            <MudTh>
                <MudTableSortLabel SortLabel="FinishedAt" T="WorkflowInstanceRow">Finished</MudTableSortLabel>
            </MudTh>
            <MudTh></MudTh>
        </HeaderContent>
        <RowTemplate>
            <MudTd DataLabel="ID">@context.WorkflowInstanceId</MudTd>
            <MudTd DataLabel="Correlation ID">
                <MudTooltip Text="@context.CorrelationId">
                    <div style="text-overflow: ellipsis; overflow: hidden; max-width: 200px; white-space: nowrap;">
                        @context.CorrelationId
                    </div>
                </MudTooltip>
            </MudTd>
            <MudTd DataLabel="Workflow">
                <MudTooltip Text="@context.WorkflowDefinition.Name">
                    <div style="text-overflow: ellipsis; overflow: hidden; max-width: 200px; white-space: nowrap;">
                        @context.WorkflowDefinition.Name
                    </div>
                </MudTooltip>
            </MudTd>
            <MudTd DataLabel="Version">@context.Version</MudTd>
            <MudTd DataLabel="Name">
                <MudTooltip Text="@context.Name">
                    <div style="text-overflow: ellipsis; overflow: hidden; max-width: 200px; white-space: nowrap;">
                        @context.Name
                    </div>
                </MudTooltip>
            </MudTd>
            <MudTd DataLabel="SubStatus">
                <MudChip Color="@GetSubStatusColor(context.SubStatus)" Variant="Variant.Text">
                    @context.SubStatus
                </MudChip>
            </MudTd>
            <MudTd DataLabel="IncidentCount">
                <MudChip
                    Color="@(context.IncidentCount == 0 ? Color.Transparent : Color.Error)"
                    Text="@context.IncidentCount.ToString()"
                    Variant="Variant.Text"
                    Size="Size.Small"/>
            </MudTd>
            <MudTd DataLabel="CreatedAt">@context.CreatedAt.ToString("G", CultureInfo.InvariantCulture)</MudTd>
            <MudTd DataLabel="UpdatedAt">@context.UpdatedAt?.ToString("G", CultureInfo.InvariantCulture)</MudTd>
            <MudTd DataLabel="FinishedAt">@context.FinishedAt?.ToString("G", CultureInfo.InvariantCulture)</MudTd>
            <MudTd DataLabel="" Style="text-align:right">
                <MudMenu Icon="@Icons.Material.Filled.MoreVert">
                    <MudMenuItem Icon="@Icons.Material.Outlined.ZoomIn" OnClick="@(() => OnViewClicked(context.WorkflowInstanceId))">View</MudMenuItem>
                    <MudMenuItem Icon="@Icons.Material.Outlined.Delete" OnClick="@(() => OnDeleteClicked(context))">Delete</MudMenuItem>
                    <MudDivider DividerType="DividerType.FullWidth"></MudDivider>
                    <MudMenuItem Icon="@Icons.Material.Outlined.FileDownload" OnClick="@(() => OnDownloadClicked(context))">Download</MudMenuItem>
                </MudMenu>
            </MudTd>
        </RowTemplate>
        <NoRecordsContent>
            <MudText>No workflow instances found</MudText>
        </NoRecordsContent>
        <LoadingContent>
            <MudText>Loading...</MudText>
        </LoadingContent>
        <PagerContent>
            <MudTablePager/>
        </PagerContent>
    </MudTable>
</MudContainer>